package com.cdck.safe.dete.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdck.safe.dete.common.result.PageResult;
import com.cdck.safe.dete.common.result.Result;
import com.cdck.safe.dete.model.entity.Activity;
import com.cdck.safe.dete.model.form.ActivityForm;
import com.cdck.safe.dete.model.query.BaseKeywordsQuery;
import com.cdck.safe.dete.service.ActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * (Activity)表控制层
 *
 * @author wp
 * @since 2024-02-26 22:57:18
 */
@Api(value = "activity")
@RestController
@RequestMapping("activity")
@RequiredArgsConstructor
public class ActivityController {

    private final ActivityService activityService;


    @ApiOperation(value = "分页列表")
    @GetMapping("/page")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "queryParams"  ,value ="查询条件" )
    })
    public PageResult<Activity> queryByPage(BaseKeywordsQuery queryParams) {
        IPage<Activity> result = activityService.queryByPage(queryParams);
        return PageResult.success(result);

    }

    @ApiOperation(value = "表单数据")
    @GetMapping("/{id}/form")
    public Result<Activity> getActivityForm(
           @PathVariable Long id
    ) {
        Activity formData = activityService.getActivityFormData(id);
        return Result.success(formData);
    }


    /**
     * 新增数据
     *
     * @param activityForm 实体
     * @return 新增结果
     */
    @ApiOperation(value = "新增")
    @PostMapping
    public Result add(
        @RequestBody @Valid
        ActivityForm activityForm
        ) {
        List<LocalDateTime> timeList = activityForm.getTimeList();
        activityForm.setStartTime(timeList.get(0));
        activityForm.setEndTime(timeList.get(1));
        Activity activity = BeanUtil.copyProperties(activityForm, Activity.class);
        boolean result = this.activityService.saveActivity(activity);
        return Result.judge(result);
    }

    @ApiOperation(value = "修改")
    @PutMapping(value = "/{id}")
    public Result updateActivity(
            @PathVariable Long id,
            @RequestBody @Validated ActivityForm activityForm) {
        List<LocalDateTime> timeList = activityForm.getTimeList();
        activityForm.setStartTime(timeList.get(0));
        activityForm.setEndTime(timeList.get(1));
        Activity activity = BeanUtil.copyProperties(activityForm, Activity.class);
        boolean result = activityService.updateActivity(id, activity);
        return Result.judge(result);
    }


    @ApiOperation(value = "删除数据")
    @DeleteMapping("/{ids}")
    public Result deleteActivitys(
             @PathVariable String ids
    ) {
        boolean result = activityService.deleteActivitys(ids);
        return Result.judge(result);
    }

}

